package com.wzy.facetextview.widget;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.text.Html;
import android.text.Html.ImageGetter;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.util.Log;
import android.widget.TextView;

import com.wzy.facetextview.Utils.ConstantsUtil;

public class FaceTextView extends TextView {

    private static final String TAG = "wang.FaceTextView";
    private Context mContext;

    public FaceTextView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        mContext = context;
    }

    public FaceTextView(Context context, AttributeSet attrs) {
        this(context, attrs, -1);
    }

    public FaceTextView(Context context) {
        this(context, null);
    }

    public void setText(String text) {

        if (TextUtils.isEmpty(text)) {
            Log.i(TAG, "text is null");
            text = "";
        }

        super.setText(Html.fromHtml(text, mImageGetter, null));
    }

    private ImageGetter mImageGetter = new ImageGetter() {

        @Override
        public Drawable getDrawable(String source) {

            Drawable drawable = null;

            if (!TextUtils.isEmpty(source)) {

                if (source.contains(ConstantsUtil.COMPAT_FACE_KEY)) {
                    drawable = FaceManager.getCompatFace(mContext, source);
                } else {
                    drawable = FaceManager.getFace(mContext, source);
                }
            }

            return drawable;
        }

    };

}
